package com.cskaoyan.demo7.servlet;

import com.cskaoyan.demo7.bean.PageData;
import com.cskaoyan.demo7.common.CommonServlet;
import com.cskaoyan.demo7.mapper.MarketRegionMapper;
import com.cskaoyan.demo7.model.MarketRegion;
import com.cskaoyan.demo7.util.JacksonUtil;
import com.cskaoyan.demo7.util.MyBatisUtil;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * GET http://localhost:8083/admin/region/list HTTP/1.1
 * <p>
 * 端口号是8083  http port 改为8083
 * 虚拟映射的时候 配置application context /
 * 最终提供的是ROOT.xml
 *
 * 相应报文的JSON字符:可以使用浏览器的开发人员工具查看
 * 这里这个data的格式是用来做分页，前端根据这些数据做分页
 * {
 *   "data":{
 *       "limit":31,
 *       "list":[],
 *       "page":1,
 *       "pages":1
 *       "total":31
 *   },
 *   "errmsg":"成功",
 *   "errno":0
 * }
 * 你要写对应的键值对信息 → 前端就是根据这些key来解析你的JSON字符串响应数据
 */
@WebServlet("/admin/region/*")
public class AdminRegionServlet extends CommonServlet {
    @SneakyThrows
    public void list(HttpServletRequest request, HttpServletResponse response) {
        PageData pageData = new PageData();
        pageData.setErrmsg("成功");
        pageData.setErrno(0);
        pageData.setData(data());

        String jsonStr = JacksonUtil.write(pageData);
        response.getWriter().println(jsonStr);
    }

    private PageData.Data data() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketRegionMapper regionMapper = sqlSession.getMapper(MarketRegionMapper.class);
        List<MarketRegion> provinceList = regionMapper.selectByType((byte) 1);
        List<MarketRegion> cityList = regionMapper.selectByType((byte) 2);
        List<MarketRegion> areaList = regionMapper.selectByType((byte) 3);

        Map<Integer, List<MarketRegion>> cityIdAreaListMap = areaList.stream().collect(Collectors.groupingBy(new Function<MarketRegion, Integer>() {
            @Override
            public Integer apply(MarketRegion marketRegion) {
                return marketRegion.getPid();
            }
        }));

        cityList.forEach(new Consumer<MarketRegion>() {
            @Override
            public void accept(MarketRegion marketRegion) {
                marketRegion.setChildren(cityIdAreaListMap.get(marketRegion.getId()));
            }
        });

        Map<Integer, List<MarketRegion>> provinceIdAreaListMap = cityList.stream().collect(Collectors.groupingBy(MarketRegion::getPid));
        provinceList.forEach(province->province.setChildren(provinceIdAreaListMap.get(province.getId())));

        //封装信息
        PageData.Data data = new PageData.Data();
        //每页数量
        data.setLimit(provinceList.size());
        //当前页具体的数据
        data.setList(provinceList);
        //当前页码
        data.setPage(1);
        //总页码
        data.setPages(1);
        //总数据量
        data.setTotal(provinceList.size());

        sqlSession.close();
        return data;


    }
}
